package com.yuke.cloud.service.wmc.service.impl;

import com.yuke.cloud.common.core.support.BaseService;
import com.yuke.cloud.common.util.wrapper.Wrapper;
import com.yuke.cloud.service.api.uac.dto.AccEmployeeInfoDto;
import com.yuke.cloud.service.api.uac.dto.SupplierApiDto;
import com.yuke.cloud.service.api.uac.dto.UserInfoDto;
import com.yuke.cloud.service.api.uac.service.UacUserFeignApi;
import com.yuke.cloud.service.wmc.dao.WmsPurchaseReturnOrderMapper;
import com.yuke.cloud.service.wmc.dto.WmsPurchaseReturnOrderDto;
import com.yuke.cloud.service.wmc.entity.WmsPurchaseReturnOrder;
import com.yuke.cloud.service.wmc.entity.WmsStockOut;
import com.yuke.cloud.service.wmc.entity.WmsStorage;
import com.yuke.cloud.service.wmc.service.WmsPurchaseReturnOrderService;
import com.yuke.cloud.service.wmc.service.WmsStockOutService;
import com.yuke.cloud.service.wmc.service.WmsStorageService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


/**
 * Created by zn on 2020/10/12.
 */
@Service
@Transactional
public class WmsPurchaseReturnOrderServiceImpl extends BaseService<WmsPurchaseReturnOrder> implements WmsPurchaseReturnOrderService {
    @Resource
    private WmsPurchaseReturnOrderMapper wmsPurchaseReturnOrderMapper;

    @Resource
    private UacUserFeignApi uacUserFeignApi;

    @Resource
    private WmsStockOutService wmsStockOutService;

    @Resource
    private WmsStorageService wmsStorageService;


    @Override
    public List<WmsPurchaseReturnOrderDto> selectCondition(WmsPurchaseReturnOrderDto wmsPurchaseReturnOrderDto) {

        List<WmsPurchaseReturnOrderDto> list = wmsPurchaseReturnOrderMapper.selectCondition(wmsPurchaseReturnOrderDto);
        if (list == null || list.size() < 1){
            return list;
        }

        List<Long> supplierIds = list.stream().map(WmsPurchaseReturnOrderDto::getSupplierId).collect(Collectors.toList());
        List<SupplierApiDto> supplierList = uacUserFeignApi.findSupplierByIds(supplierIds);
        if (supplierList == null || supplierList.size() < 1){
            return list;
        }
        AccEmployeeInfoDto accEmployeeInfoDto = new AccEmployeeInfoDto();
        accEmployeeInfoDto.setEmployeeType((byte)0);
        Wrapper<List<UserInfoDto>> userWrapper =uacUserFeignApi.selectEmployInfo(accEmployeeInfoDto);
        if (userWrapper.getCode() != 200){
            return list;
        }
        List<UserInfoDto> userList = userWrapper.getResult();

        if (supplierList.size()>0 && supplierList!=null) {
            for (WmsPurchaseReturnOrderDto purchaseReturnOrderDto : list) {
                for (SupplierApiDto supplierApiDto : supplierList) {
                    if (purchaseReturnOrderDto.getSupplierId().equals(supplierApiDto.getSupplierId())) {
                        BeanUtils.copyProperties(supplierApiDto, purchaseReturnOrderDto);
                    }
                }
                for (UserInfoDto userInfoDto : userList){
                    if (purchaseReturnOrderDto.getUpdateId().equals(userInfoDto.getUserId())){
                        purchaseReturnOrderDto.setUserName(userInfoDto.getUserName());
                        break;
                    }
                }

            }
        }
        return list;
    }

    @Override
    public Integer deleteStorageByReturn(Long id) {
        //修改出库记录
        Example example = new Example(WmsStockOut.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("purchaseReturnId", id);
        List<WmsStockOut> list = wmsStockOutService.selectByExample(example);

        for (WmsStockOut stockOut : list) {
            //根据list 更新wmsStorage表内库存数
            WmsStorage wmsStorage = new WmsStorage();
            wmsStorage.setSpecId(stockOut.getSpecId());
            wmsStorage.setWarehouseId(stockOut.getWarehouseId());
            List<WmsStorage> select = wmsStorageService.select(wmsStorage);
            wmsStorage = select.get(0);
            Integer number = wmsStorage.getNumber() + stockOut.getOutNumber();
            wmsStorage.setNumber(number);//进货退货取消后 应该增加库存
            wmsStorage.setUpdateTime(new Date());
            Integer update = wmsStorageService.update(wmsStorage);
            if (update.equals(0)) {
                return 0;
            }
        }
        return 1;
    }


}
